﻿using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using YKXiangQi.Adapters;
using YKXiangQi.Common;
using YKXiangQi.Enums;
using System;

namespace YKXiangQi.Models
{
    [Serializable]
    public class Engine: Player
    {
        public bool IsStarted
        {
            get { return Adapter.IsStarted; }
        }

        public Engine()
        {
            Settings = new EngineSettings();
        }

        public EngineSettings Settings { get; set; }

        public override PlayerType Type
        {
            get { return PlayerType.Computer;}
        }

        public override string Name
        {
            get { return Settings.Name; }
        }

        public static IList<Engine> LoadEnginesFromSettings()
        {
            var engines = Properties.Settings.Default.Engines;
            if (!string.IsNullOrEmpty(engines))
            {
                var result = engines.ToArray().Deserialize<IList<Engine>>();
                return result;
            }

            return new List<Engine>();
        }

        public string Tooltip
        {
            get { return Settings.Name + "\r\n" + Settings.Path; }
        }

        [NonSerialized]
        private EngineAdapter _adapter;
        public EngineAdapter Adapter
        {
            get
            {
                if (_adapter == null)
                {
                    switch (Settings.Protocol)
                    {
                        case EngineProtocol.UCI:
                            {
                                _adapter = new UciEngineAdapter() {Settings = this.Settings};
                                break;
                            }
                        case EngineProtocol.UCCI:
                            {
                                _adapter = new UcciEngineAdapter() {Settings = this.Settings};
                                break;
                            }
                    }
                }

                return _adapter;
            }
        }
    }
}
